//
//  YLGateVC.m
//  HHPlateNum_Example
//
//  Created by 徐国梁 on 2020/7/30.
//  Copyright © 2020 蒋明明. All rights reserved.
//

#import "YLGateVC.h"
#import <HHWTPlateIDCameraViewController.h>
#import "AFNetworking.h"

@interface YLGateVC ()
@property (nonatomic, strong) UILabel* plateNumLabel;
@end

@implementation YLGateVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self addSubviews];
}

- (void)dealloc {
    
}

//MARK: - 通知消息
- (void)enterNoti: (NSNotification *)noti {
    NSLog(@"%@", noti.object);
    NSLog(@"%@", noti.userInfo);
}

//MARK: - 视图和操作
- (void)addSubviews {
    [self.view addSubview:self.plateNumLable];
    
    CGFloat screenWidth = UIScreen.mainScreen.bounds.size.width;
    CGFloat startX = (screenWidth-150)/2;
    NSArray* title = @[@"进入", @"查询", @"支付", @"离开"];
    for (int i = 0; i < title.count; i++) {
        UIButton* button = [[UIButton alloc] initWithFrame: CGRectMake(startX, self.plateNumLable.frame.origin.y+45+(50+10)*i, 150, 50)];
        button.backgroundColor = [UIColor lightGrayColor];
        [button setTitle: title[i] forState: UIControlStateNormal];
        [self.view addSubview:button];
        button.tag = 300+i;
        [button addTarget:self action:@selector(HHButtonClick:) forControlEvents: UIControlEventTouchUpInside];
    }
}

-(UILabel*)plateNumLable {
    if (!_plateNumLabel) {
        _plateNumLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, self.view.frame.size.width, 25)];
        _plateNumLabel.textAlignment = NSTextAlignmentCenter;
        _plateNumLabel.textColor = [UIColor orangeColor];
    }
    return _plateNumLabel;
}

-(void)HHButtonClick:(UIButton*)button {
    if (button.tag == 300) {//进入
        HHWTPlateIDCameraViewController* vc = [[HHWTPlateIDCameraViewController alloc] init];
        vc.plateNumBlock = ^(PlateResult *model) {
            _plateNumLabel.text = model.license;
            [self enter: model.license];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }else if (button.tag == 301) {//查询
        [self queryCost];
    }else if (button.tag == 302) {//支付
        [self pay];
    }else {//离开
        HHWTPlateIDCameraViewController* vc = [[HHWTPlateIDCameraViewController alloc] init];
        vc.plateNumBlock = ^(PlateResult *model) {
            [self leave: model.license];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }
}

//MARK: - 接口
//MARK: 查询停车费
- (void)queryCost {
    if (_plateNumLabel.text == NULL) {
        NSLog(@"请先扫码进入停车场");
        return;
    }
    //urlStr
    NSString *urlStr = @"http://192.168.124.2:10087/parking/queryCost";
    //可变字典(存储请求参数format=json)
    NSMutableDictionary *mutDic = [NSMutableDictionary dictionary];
    mutDic[@"plateNumber"] = _plateNumLabel.text;
    
    AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];
    //manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];//[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/xml", @"text/plain", nil];
    [manager POST:urlStr parameters:mutDic progress:^(NSProgress * _Nonnull downloadProgress) {
        // 进度
        NSLog(@"进度");
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"请求成功");
        NSLog(@"%@", responseObject);
        //NSInteger stateCode = [responseObject[@"code"] intValue];
//        NSLog(@"%ld",(long)stateCode);
//        switch (stateCode) {
//            case 1:{
//                //_dataArr = sumDic[@"data"];
//                //[self.tableView reloadData];
//                break;
//            }
//            case 0:{
//                //[MBProgressHUD showMessage:sumDic[@"message"]];
//                break;
//            }
//            default:
//                break;
//        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败%@", error);
    }];
}
//MARK: 支付停车费
- (void)pay {
    if (_plateNumLabel.text == NULL) {
        NSLog(@"请先扫码进入停车场");
        return;
    }
    //urlStr
    NSString *urlStr = @"http://192.168.124.2:10087/parking/pay";
    //可变字典(存储请求参数format=json)
    NSMutableDictionary *mutDic = [NSMutableDictionary dictionary];
    mutDic[@"plateNumber"] = _plateNumLabel.text;
    
    AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];
    //manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];//[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/xml", @"text/plain", nil];
    [manager POST:urlStr parameters:mutDic progress:^(NSProgress * _Nonnull downloadProgress) {
        // 进度
        NSLog(@"进度");
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"请求成功");
        NSLog(@"%@", responseObject);
        //NSInteger stateCode = [responseObject[@"code"] intValue];
//        NSLog(@"%ld",(long)stateCode);
//        switch (stateCode) {
//            case 1:{
//                //_dataArr = sumDic[@"data"];
//                //[self.tableView reloadData];
//                break;
//            }
//            case 0:{
//                //[MBProgressHUD showMessage:sumDic[@"message"]];
//                break;
//            }
//            default:
//                break;
//        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败%@", error);
    }];
}
//MARK: 进入停车场
- (void)enter:(NSString *) plateNumber {
    //urlStr
    NSString *urlStr = @"http://192.168.124.2:10087/parking/enter";
    //可变字典(存储请求参数format=json)
    NSMutableDictionary *mutDic = [NSMutableDictionary dictionary];
    mutDic[@"plateNumber"] = plateNumber;
    
    AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];//[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/xml", @"text/plain", nil];
    [manager POST:urlStr parameters:mutDic progress:^(NSProgress * _Nonnull downloadProgress) {
        // 进度
        NSLog(@"进度");
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"请求成功");
        NSLog(@"%@", responseObject);
        //NSInteger stateCode = [responseObject[@"code"] intValue];
//        NSLog(@"%ld",(long)stateCode);
//        switch (stateCode) {
//            case 1:{
//                //_dataArr = sumDic[@"data"];
//                //[self.tableView reloadData];
//                break;
//            }
//            case 0:{
//                //[MBProgressHUD showMessage:sumDic[@"message"]];
//                break;
//            }
//            default:
//                break;
//        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败%@", error);
    }];
}
//MARK: - 离开停车场
- (void)leave:(NSString *) plateNumber {
    //urlStr
    NSString *urlStr = @"http://192.168.124.2:10087/parking/leave";
    //可变字典(存储请求参数format=json)
    NSMutableDictionary *mutDic = [NSMutableDictionary dictionary];
    mutDic[@"plateNumber"] = plateNumber;
    
    AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];//[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/xml", @"text/plain", nil];
    [manager POST:urlStr parameters:mutDic progress:^(NSProgress * _Nonnull downloadProgress) {
        // 进度
        NSLog(@"进度");
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"请求成功");
        NSLog(@"%@", responseObject);
        //NSInteger stateCode = [responseObject[@"code"] intValue];
//        NSLog(@"%ld",(long)stateCode);
//        switch (stateCode) {
//            case 1:{
//                //_dataArr = sumDic[@"data"];
//                //[self.tableView reloadData];
//                break;
//            }
//            case 0:{
//                //[MBProgressHUD showMessage:sumDic[@"message"]];
//                break;
//            }
//            default:
//                break;
//        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败%@", error);
    }];
}
@end
